# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Steve Holweg <skytep@gmail.com>
# Contributor: Baptiste Jonglez <baptiste--aur@jonglez.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
pkgname=kea
# NOTE: Upgrade only to even-numbered versions (e.g. 1.8.x, 1.10.x)!
#   Odd-numbered versions are development versions.
# NOTE: When bumping, always rebuild all kea hooks (kea-* aports)!
pkgver=2.4.1
pkgrel=2
pkgdesc="DHCPv4 and DHCPv6 server from ISC"
url="https://kea.isc.org/"
arch="all"
license="MPL-2.0"
depends="kea-dhcp4 kea-dhcp6"
depends_dev="boost-dev log4cplus-dev"
checkdepends="procps"
makedepends="
	$depends_dev
	autoconf
	automake
	bison
	botan-dev
	flex
	libcap-utils
	libtool
	mariadb-dev
	postgresql-dev
	python3-dev
	"
pkgusers="kea"
pkggroups="kea"
install="$pkgname-common.pre-install $pkgname-common.post-upgrade"
subpackages="
	$pkgname-doc
	$pkgname-static
	$pkgname-dev
	$pkgname-admin::noarch
	$pkgname-ctrl-agent:ctrlagent
	$pkgname-dhcp-ddns:dhcpddns
	$pkgname-dhcp4
	$pkgname-dhcp6
	$pkgname-shell-pyc
	$pkgname-shell::noarch
	$pkgname-hook-mysql-cb:hook_mysql_cb
	$pkgname-hook-pgsql-cb:hook_pgsql_cb
	$pkgname-hook-ha:hook_ha
	$pkgname-hook-stat-cmds:hook_stat_cmds
	$pkgname-hook-lease-cmds:hook_lease_cmds
	$pkgname-hook-flex-option:hook_flex_option
	$pkgname-hook-bootp:hook_bootp
	$pkgname-hook-run-script:hook_run_script
	$pkgname-common
	"
source="https://ftp.isc.org/isc/kea/$pkgver/kea-$pkgver.tar.gz
	disable-db-tests.patch
	configs-fix-paths.patch
	put-LOCKFILE_DIR-to-runstatedir.patch
	move-api-files-to-doc.patch
	kea-admin-remove-builddir.patch
	python3.12.patch
	kea.initd.in
	kea.confd.in
	"
validpgpkeys="BE0E9748B718253A28BB89FFF1B11BF05CF02E57" # Internet Systems Consortium, Inc. (Signing key, 2017-2018) <codesign@isc.org>

# secfixes:
#   1.7.2-r0:
#     - CVE-2019-6472
#     - CVE-2019-6473
#     - CVE-2019-6474

# NOTE: libkea-dhcpsrv.so depends on libkea-{mysql,pgsql}, that's why
#   kea-common depends on libpq and libmariadb. :(

prepare() {
	default_prepare
	update_config_sub

	autoreconf -fv

	# Remove the builddir path from the scripts.
	# NOTE: There's a new script each release, so using patch file would be
	# very inconvenient.
	find src/share/database/scripts/ \
		-name '*.sh.in' \
		-exec sed -i 's|^\s*. @abs_top_builddir@/src/bin/admin/admin-utils.sh.*|echo "admin-utils.sh not found!"; exit 1|' {} \;
}

build() {
	# Complete build for dev and doc

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--libexecdir=/usr/lib \
		--runstatedir=/run \
		--with-mysql \
		--with-pgsql \
		--disable-rpath \
		--enable-generate-messages \
		--enable-generate-parser \
		--enable-shell
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	cd "$pkgdir"

	install -d -m 0750 -o kea -g kea ./var/log/kea ./var/lib/kea

	# keactrl is unnecessary since we provide OpenRC init scripts.
	rm ./usr/sbin/keactrl ./etc/kea/keactrl.conf

	rm -Rf ./run
}

dev() {
	default_dev

	amove ./usr/bin/kea-msg-compiler
}

admin() {
	pkgdesc="Databases administration tools for Kea DHCP server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove ./usr/sbin/kea-admin
	amove ./usr/share/kea
}

ctrlagent() {
	pkgdesc="Kea Control Agent - REST service for controlling Kea DHCP server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove usr/sbin/kea-ctrl-agent
	amove ./etc/kea/kea-ctrl-agent.conf

	_install_initd kea-ctrl-agent
}

dhcpddns() {
	pkgdesc="Kea DHCP Dynamic DNS Server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove ./usr/sbin/kea-dhcp-ddns
	amove ./etc/kea/kea-dhcp-ddns.conf
	amove ./usr/lib/libkea-asiodns.so.*
	amove ./usr/lib/libkea-d2srv.so.*

	_install_initd kea-dhcp-ddns
}

dhcp4() {
	pkgdesc="Kea IPv4 DHCP Server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove ./usr/sbin/kea-dhcp4
	amove ./etc/kea/kea-dhcp4.conf

	_install_initd kea-dhcp4
}

dhcp6() {
	pkgdesc="Kea IPv6 DHCP Server"
	depends="$pkgname-common=$pkgver-r$pkgrel"

	amove ./usr/sbin/kea-dhcp6
	amove ./etc/kea/kea-dhcp6.conf

	_install_initd kea-dhcp6
}

shell() {
	pkgdesc="Text client for Kea DHCP Control Agent"
	depends="python3"

	amove ./usr/sbin/kea-shell
	amove ./usr/lib/python3*
}

hook_mysql_cb() {
	pkgdesc="Kea MySQL Configuration Backend hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_mysql_cb.so
}

hook_pgsql_cb() {
	pkgdesc="Kea PostgreSQL Configuration Backend hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_pgsql_cb.so
}

hook_ha() {
	pkgdesc="Kea High Availability hooks library"
	depends="$pkgname-hook-lease-cmds"

	amove ./usr/lib/kea/hooks/libdhcp_ha.so
}

hook_stat_cmds() {
	pkgdesc="Kea Statistics Commands hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_stat_cmds.so
}

hook_lease_cmds() {
	pkgdesc="Kea Lease Commands hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_lease_cmds.so
}

hook_flex_option() {
	pkgdesc="Kea Flexible Option hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_flex_option.so
}

hook_bootp() {
	pkgdesc="Kea BOOTP hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_bootp.so
}

hook_run_script() {
	pkgdesc="Kea Run Script hooks library"
	depends=""

	amove ./usr/lib/kea/hooks/libdhcp_run_script.so
}

common() {
	pkgdesc="Common files and libraries for Kea DHCP Server"
	replaces="kea-utils"  # for backward compatibility
	provides="kea-utils=$pkgver-r$pkgrel kea-http=$pkgver-r$pkgrel"  # for backward compatibility
	depends=""

	mkdir -p "$subpkgdir"
	mv "$pkgdir"/* "$subpkgdir"/
}

_install_initd() {
	local name="$1"

	local caps='^cap_net_bind_service'
	case "$name" in
		kea-dhcp4) caps="$caps,^cap_net_raw";;
		kea-ctrl-agent) caps='';;
	esac

	install -Dm755 "$srcdir"/kea.initd.in "$subpkgdir"/etc/init.d/$name
	install -Dm644 "$srcdir"/kea.confd.in "$subpkgdir"/etc/conf.d/$name
	# /^capabilities... is to remove this variable when it's rendered empty (see #15048).
	sed -i \
		-e "s|@@NAME@@|$name|g" \
		-e "s|@@CAPABILITIES@@|$caps|" \
		-e '/^capabilities=""/d' \
		"$subpkgdir"/etc/init.d/$name \
		"$subpkgdir"/etc/conf.d/$name
}

sha512sums="
b8a3b6f2cae213fd9826c37568c71d3458f52eed973dbe437a1d0974dafa026635a730d828c6ff03b32e030be57d75a7914a8ca313833e91d9996b6a05b2b224  kea-2.4.1.tar.gz
9008df39883884db5fb4a6551d92262b465c186275bebd45358bdd25a637517c5613634f7541c226869dd8ca4118230726bf250b1d4c5198f3c5079527c72440  disable-db-tests.patch
3439046a80de33b31bf3f12e3c9728e47119e245fb888a1e4484a443b20e7b364f0e15e10fa96b4fcfcec29e6d2828ba9ab728a83028546a9c28f30fc404512d  configs-fix-paths.patch
52de95e9b4de3e518b4fbbce19799230684a2924433e666c24b866e537ffeaea9cd16b0b41499a17dd3ed528be1f899e94e67f6e464d551149b850cdbf29c1b2  put-LOCKFILE_DIR-to-runstatedir.patch
100b70f3ccecb46b98e9fd599ab6f0c3aa191d862d914ed84f4d34efea0cc119817d1e14b967c0f5024614af1ef6841a8d12e789945cad147d943965f194ece1  move-api-files-to-doc.patch
eef973da24aaa3dd1b02925d6be1f59caa363a7721e2e4dd6bd8553b0c351961f7436b21c32cbc6acdf90cf99a7fe7daa954c8ce4dbd0eda36844f566b126c48  kea-admin-remove-builddir.patch
8feb09cf184c3128b8f434e896f062d12e978a3fda435bb57389756cf0290ba993604fc2512101b7d7865c7be831d9fe6519c9e16885c7e737ea63513ed9135f  python3.12.patch
e2f77a403b1c84918c92acf8c22d4bd3ea3662d109c334aebb4f55f1821901fbf287f0c1ac03f5de56227e888edc63667f54a0ee51fa4b654479f4e14841d11a  kea.initd.in
eb45671073174e319cd4fedb61c5f8664370f701856569c675a5aaa280bde974bdabbc30e733379b2e61b32dc2e1e94808b79f141de7c209109e874b2b4b5e8f  kea.confd.in
"
